Unknownpgr

Importance Sampling

2023-09-01 13:53:39 | English, Korean

This post was translated from Korean into English by AI.

Importance Sampling: A method for estimating the expected value of f(x)f(x) when xp(x)x\sim p(x), where p(x)p(x) is a random variable that is difficult to sample from, by using a random variable q(x)q(x) that is easy to sample from.

Proof

Exp(x)[f(x)]=f(x)p(x)dx=f(x)p(x)q(x)q(x)dx=Exq(x)[p(x)q(x)f(x)]\begin{aligned} \mathbb{E}_{x\sim p(x)}[f(x)] &= \int f(x)p(x)dx \\ &= \int f(x)\frac{p(x)}{q(x)}q(x)dx \\ &= \mathbb{E}_{x\sim q(x)}\left[\frac{p(x)}{q(x)}f(x)\right] \end{aligned}

Example

For the probability distribution p(x)=ex2πp(x)=\frac{e^{-x^2}}{\sqrt{\pi}}, suppose we want to estimate the expected value of f(x)=x2f(x)=x^2 when xp(x)x\sim p(x).

Since p(x)p(x) is difficult to integrate, it is not easy to sample from it.

Therefore, let us estimate the expected value of f(x)f(x) by sampling from q(x)=N(0,1)q(x)=\mathcal{N}(0,1).

We can estimate the expected value with the following code.

import numpy as np


def func(x):
    return x**2


def p(x):
    return np.exp(-(x**2)) / (np.sqrt(np.pi))


def q(x):
    # Return proboability density function of a normal distribution
    return np.exp(-(x**2) / 2) / (np.sqrt(2 * np.pi))


# Sample from a normal distribution, sample size = 10000
mu = 0
sigma = 1
sample_size = 100000
sample = np.random.normal(mu, sigma, sample_size)

# Calculate the expectation
expectation = np.mean(func(sample) * p(sample) / q(sample))
print(expectation)

The output is 0.5008740539678816, which is very close to 0.5.


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -